package com.jy.adscm.operation.pebase.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeAttachment;
import com.jy.adscm.operation.pebase.service.PeAttachmentService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-19
 */
@RestController
@RequestMapping("/adscm/pebase/peAttachment")
public class PeAttachmentController {
    @Value("${pebase.upload-path}")
    private String  xjFilePath;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    PeAttachmentService peAttachmentService;
    @RequestMapping("upload")
    public SimpleCoreResponse insert( HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        String xjAttachmentstr = request.getParameter("peAttachment");
        if (xjAttachmentstr==null||"".equals(xjAttachmentstr)){
            throw  new AdscmSystemException("peAttachment 没得");
        }
        ObjectMapper mapper = new ObjectMapper();
        PeAttachment peAttachment = mapper.readValue(xjAttachmentstr, PeAttachment.class);
        List<String> paths = null;
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        if(multipartResolver.isMultipart(request)){
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

            String path = xjFilePath /*+ File.separator*/ + UUID.randomUUID().toString().replaceAll("-","") + File.separator;

            paths = AdscmSystemUtils.getInstance().uploadFileNoNewName(multiRequest,path);
        }
        if (paths!=null){
            String path = paths.get(0);
            peAttachment.setFilePath(path);
            String  fileName =  path.substring(path.lastIndexOf("\\") + 1);
            peAttachment.setFileName(fileName);
            peAttachment.setUploader(token.getUser().getId().toString());
            peAttachment.setUploadTime(new Date());
        }
        boolean insert = peAttachmentService.insert(peAttachment);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("download/{id}")
    public  SimpleCoreResponse downloadSjAttachment(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response)throws AdscmSystemException {
        PeAttachment peAttachment = peAttachmentService.selectById(id);
        if (peAttachment==null){
            throw  new AdscmSystemException("没找到文件");
        }
        AdscmSystemUtils.getInstance().downloadFile(peAttachment.getFilePath(),response);
        return new SimpleCoreResponse("200","下载成功",null);
    }

    @RequestMapping("delete/{id}")
    public  SimpleCoreResponse delete(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response)throws AdscmSystemException {
        PeAttachment peAttachment = peAttachmentService.selectById(id);
        if (peAttachment==null){
            throw  new AdscmSystemException("没找到文件");
        }
        String filePath = peAttachment.getFilePath();
        File file =new File(filePath);
        if (file.exists()){
            file.delete();
        }
        boolean b = peAttachmentService.deleteById(id);
        return new SimpleCoreResponse("200","删除成功",b);
    }
    @RequestMapping("selectByDid/{did}")
    public  SimpleCoreResponse selectByDid(@PathVariable Integer did,HttpServletRequest request, HttpServletResponse response)throws AdscmSystemException {

        List<PeAttachment> peAttachments = peAttachmentService.selectByDid(did);
        return new SimpleCoreResponse("200","查询成功",peAttachments);
    }


}

